<?php
if (!defined('IN_IPB')) exit("<h1>Incorrect access</h1> You may not access this file without the acp.");

class gametype_match extends gametype
{
	var $name = "Matching";
	
	function _xml_to_info(&$xml_class)
	{
		parent::_xml_to_info($xml_class);
		
		$this->card_info['prizes']['probability'] = $xml_class->xml_array['scratchcardexport']['gametype']['info']['probability']['VALUE'];
		$this->card_info['prizes']['match'] = $xml_class->xml_array['scratchcardexport']['gametype']['info']['match']['VALUE'];
		$this->card_info['prizes']['money'] = $xml_class->xml_array['scratchcardexport']['gametype']['info']['money']['VALUE'];
		$this->card_info['prizes']['items'] = array();
	}
	
	function export(&$xml_class)
	{
		$xml_class->xml_add_group('gametype');
		$entry = array();
		$content = array();
		$content[] = $xml_class->xml_build_simple_tag('probability',$this->card_info['prizes']['probability']);
		$content[] = $xml_class->xml_build_simple_tag('match',$this->card_info['prizes']['match']);
		$content[] = $xml_class->xml_build_simple_tag('money',$this->card_info['prizes']['money']);
		$entry[] = $xml_class->xml_build_entry('info',$content);
		$xml_class->xml_add_entry_to_group('gametype',$entry);
	}
	
	function admin_form()
	{
		$dropdown = array();
		for($i = 2; $i <= count($this->card_info['choice_info']); $i++)
		{
			$dropdown[] = array($i,$i);
		}
		
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "60%" );
		
		$html .= $this->ipsclass->adskin->start_table("Prizes");
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>Probability of winning</b>",
															  $this->ipsclass->adskin->form_simple_input('prizes[probability]',$this->card_info['prizes']['probability'])."%"
													 )      );
		
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>Number of Matches</b>",
															  $this->ipsclass->adskin->form_dropdown('prizes[match]', $dropdown, $this->card_info['prizes']['match'])
													 )      );
		
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>Prize money</b>",
															  $this->ipsclass->vars['market_prefix'].$this->ipsclass->adskin->form_simple_input('prizes[money]', $this->card_info['prizes']['money']).$this->ipsclass->vars['market_suffix']
													 )      );
		
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>Prize Items</b>",
															  $this->ipsclass->market_admin->compile_item_list("prizes[items]",$this->card_info['prizes']['items'])
													 )      );
		
		$html .= $this->ipsclass->adskin->end_table();
		
		return $html;
	}
	
	function admin_save(&$card)
	{
		$card['prizes'] = array();
		$card['prizes']['probability'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['prizes']['probability']);
		$card['prizes']['match'] = max(min(intval($this->ipsclass->input['prizes']['match']),count($this->card_info['choice_info'])),0);
		$card['prizes']['money'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['prizes']['money'],'int');
		$card['prizes']['items'] = $this->ipsclass->market_admin->parse_input_item_list(array('prizes','items'));
	}
	
	function is_going_to_win()
	{
		return $this->ipsclass->numbers->compare($this->ipsclass->numbers->random_number(0,100,1),$this->card_info['prizes']['probability']) <= 0 ? 1 : 0;
	}
	
	function create_choices($win = 0)
	{
		$choices = array();
		$card = $this->card_info;
		if($win)
		{
			$win_key = array_rand($card['choice_imgs']);
			$win_image = $card['choice_imgs'][$win_key];
			unset($card['choice_imgs'][$win_key]);
			
			for($i = 0; $i < $this->card_info['prizes']['match']; $i++)
			{
				$choices[$i] = $win_image;
			}
		}
		$count = array();
		for($i = count($choices); $i < count($card['choice_info']); $i++)
		{
			$k = array_rand($card['choice_imgs']);
			$image = $card['choice_imgs'][$k];
			$choices[$i] = $image;
			
			if(++$count[$image] == $this->card_info['prizes']['match'] - 1)
			{
				unset($card['choice_imgs'][$k]);
			}
		}
		
		shuffle($choices);
		return $choices;
	}
	
	function money_reward($win = 0)
	{
		return $win ? $this->card_info['prizes']['money'] : 0;
	}
	
	function items_reward($win = 0)
	{
		return $win ? $this->card_info['prizes']['items'] : array();
	}
}
?>